package com.example.car;

import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.TranslateAnimation;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.ViewFlipper;

public class CarsActivity extends Activity implements OnTouchListener {

	float downXValue = 0;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.real);

		//RelativeLayout layMain = (RelativeLayout) findViewById(R.id.car_layout_main);
		//layMain.setOnTouchListener((OnTouchListener) this);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_cars, menu);
		return true;
	}

	@Override
	public boolean onTouch(View v, MotionEvent event) {
		// TODO Auto-generated method stub
		// Get the action that was done on this touch event
		switch (event.getAction()) {
		case MotionEvent.ACTION_DOWN: {
			// store the X value when the user's finger was pressed down
			downXValue = event.getX();
			Log.d("LOG", "MotionEvent.ACTION_DOWN");
			ViewFlipper vf = (ViewFlipper) findViewById(R.id.car_viewFlipper);
			// Set the animation
			vf.setInAnimation(inFromRightAnimation());
			vf.setOutAnimation(outToLeftAnimation());
			// Flip!
			vf.showNext();
			// Log.d("LOG","showNext");
			break;
		}

		case MotionEvent.ACTION_UP: {
			// Get the X value when the user released his/her finger
			float currentX = event.getX();
			Log.d("LOG", "MotionEvent.ACTION_UP");
			// going backwards: pushing stuff to the right
			if (downXValue < currentX) {
				// Get a reference to the ViewFlipper
				ViewFlipper vf = (ViewFlipper) findViewById(R.id.car_viewFlipper);
				// Set the animation
				vf.setAnimation(AnimationUtils.loadAnimation(this,
						R.anim.push_left_in));
				// Flip!
				vf.showPrevious();
				Log.d("LOG", "showPrevious");
			}

			// going forwards: pushing stuff to the left
			if (downXValue > currentX) {
				// Get a reference to the ViewFlipper
				ViewFlipper vf = (ViewFlipper) findViewById(R.id.car_viewFlipper);
				// Set the animation
				vf.setInAnimation(AnimationUtils.loadAnimation(this,
						R.anim.push_left_in));
				// Flip!
				vf.showNext();
				Log.d("LOG", "showNext");
			}
			break;
		}
		}

		return false;
	}

	private Animation inFromRightAnimation() {

		Animation inFromRight = new TranslateAnimation(
				Animation.RELATIVE_TO_PARENT, +1.0f,
				Animation.RELATIVE_TO_PARENT, 0.0f,
				Animation.RELATIVE_TO_PARENT, 0.0f,
				Animation.RELATIVE_TO_PARENT, 0.0f);
		inFromRight.setDuration(1000);
		inFromRight.setInterpolator(new AccelerateInterpolator());
		return inFromRight;
	}

	private Animation outToLeftAnimation() {
		Animation outtoLeft = new TranslateAnimation(
				Animation.RELATIVE_TO_PARENT, 0.0f,
				Animation.RELATIVE_TO_PARENT, -1.0f,
				Animation.RELATIVE_TO_PARENT, 0.0f,
				Animation.RELATIVE_TO_PARENT, 0.0f);
		outtoLeft.setDuration(1000);
		outtoLeft.setInterpolator(new AccelerateInterpolator());
		return outtoLeft;
	}

	private Animation inFromLeftAnimation() {
		Animation inFromLeft = new TranslateAnimation(
				Animation.RELATIVE_TO_PARENT, -1.0f,
				Animation.RELATIVE_TO_PARENT, 0.0f,
				Animation.RELATIVE_TO_PARENT, 0.0f,
				Animation.RELATIVE_TO_PARENT, 0.0f);
		inFromLeft.setDuration(1000);
		inFromLeft.setInterpolator(new AccelerateInterpolator());
		return inFromLeft;
	}

	private Animation outToRightAnimation() {
		Animation outtoRight = new TranslateAnimation(
				Animation.RELATIVE_TO_PARENT, 0.0f,
				Animation.RELATIVE_TO_PARENT, +1.0f,
				Animation.RELATIVE_TO_PARENT, 0.0f,
				Animation.RELATIVE_TO_PARENT, 0.0f);
		outtoRight.setDuration(1000);
		outtoRight.setInterpolator(new AccelerateInterpolator());
		return outtoRight;
	}
}
